package com.chcplayer.ui
{
import com.chcplayer.ChcPlayerApp;
import com.chcplayer.enums.FormCloseType;

import flash.desktop.NotificationType;
import flash.display.NativeWindow;
import flash.display.NativeWindowInitOptions;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;

/**
 * author yh
 */
public class BaseWindow extends Sprite
{
	/**
	 */
	public static const BUTTON_X:int = 12;
	/**
	 */
	public static const BTN_SLOT:int = 10;
	/**
	 */
	protected var p_strCloseFlag:String;
	/**
	 */
	protected var p_btnDefault:CHSimpleButton = new CHSimpleButton();
	/**
	 */
	protected var p_btnCancel:CHSimpleButton = new CHSimpleButton();
	/**
	 */
	protected var p_btnOK:CHSimpleButton = new CHSimpleButton();
	/**
	 */
	protected var p_obApp:ChcPlayerApp;
//==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
// protected
//==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
	/**
	 */
	private var m_obNWIO:NativeWindowInitOptions;
	/**
	 */
	private var m_strT:String;
	/**
	 */
	private var m_nX:int;
	private var m_nY:int;
	/**
	 */
	private var m_nW:int;
	private var m_nH:int;
	/**
	 */
	public function BaseWindow(strT:String="", nW:int=500,nH:int=500)
	{
		m_nW = nW;
		m_nH = nH;
		m_strT = strT;
		m_obNWIO = this._InitNWIO();
		/*
		*/
		p_btnDefault.setText( "恢复默认" );
		p_btnCancel.setText( "取消" );
		p_btnOK.setText( "确定" );
		p_btnDefault.setWidth( 100 );
		p_btnCancel.setWidth( 80 );
		p_btnOK.setWidth( 80 );
		/*
		*/
		p_btnOK.x = BUTTON_X;
		p_btnCancel.x = p_btnOK.x + p_btnOK.width + BTN_SLOT;
		/*
		*/
		p_btnDefault.addEventListener(MouseEvent.CLICK, click_btn);
		p_btnCancel.addEventListener(MouseEvent.CLICK, click_btn);
		p_btnOK.addEventListener(MouseEvent.CLICK, click_btn);
		this.addEventListener(Event.ADDED_TO_STAGE, toStage_this);
	}
//==================================================================================================
// public
//==================================================================================================
	/**
	 */
	public function centerTo(nw:NativeWindow):void
	{
		m_nX = nw.x + ( (nw.width-m_nW)>>1 );
		m_nY = nw.y + ( (nw.height-m_nH)>>1 );
	}
	/**
	 */
	public function show():void
	{
		if ( this.stage != null )
		{
			this.stage.nativeWindow.notifyUser(NotificationType.INFORMATIONAL);
		}
		else
		{
			var nw:NativeWindow = new NativeWindow(m_obNWIO);
			nw.alwaysInFront = true;
			nw.stage.scaleMode = StageScaleMode.NO_SCALE;
			nw.stage.align = StageAlign.TOP_LEFT;
			nw.stage.addChild(this);
			nw.x = m_nX;
			nw.y = m_nY;
			nw.width = m_nW;
			nw.height = m_nH;
			nw.addEventListener(Event.CLOSE, close_nw);
			nw.title = m_strT;
			nw.activate();
		}
		this._OnShow();
	}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// events
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	private function toStage_this(e:Event):void
	{
		this.stage.addEventListener(Event.RESIZE, resize_stage);
		this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown_stage);
	}
	/**
	 */
	private function click_btn(e:MouseEvent):void
	{
		if ( e.currentTarget == p_btnDefault )
		{
			this._ToDefault();
		}
		else if ( e.currentTarget == p_btnCancel )
		{
			p_strCloseFlag = FormCloseType.CANCEL;
			this.close();
		}
		else if ( e.currentTarget == p_btnOK )
		{
			p_strCloseFlag = FormCloseType.CONFIRM;
			this.close();
		}
	}
	/**
	 */
	private function keyDown_stage(e:KeyboardEvent):void
	{
		if ( Keyboard.ENTER == e.keyCode )
		{
			p_strCloseFlag = FormCloseType.CONFIRM;
			this.close();
		}
		else if ( Keyboard.ESCAPE == e.keyCode )
		{
			p_strCloseFlag = FormCloseType.CANCEL;
			this.close();
		}
	}
	/**
	 */
	private function resize_stage(e:Event):void
	{
		this.graphics.clear();
		this.graphics.beginFill(0xd4d0c8);
		this.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
		this.graphics.endFill();
	}
	/**
	 */
	private function close_nw(e:Event):void
	{
		NativeWindow(e.target).removeEventListener(Event.CLOSE, close_nw);
		this.stage.removeEventListener(Event.RESIZE, resize_stage);
		this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDown_stage);
		this.stage.removeChild(this);
		this._OnClose();
		this.dispatchEvent(e);
	}
//==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
// protected
//==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
	/**
	 */
	protected function _OnClose():void
	{
		
	}
	/**
	 */
	protected function _ToDefault():void
	{
		
	}
	/**
	 */
	protected function _OnShow():void
	{
		
	}
	/**
	 */
	protected function _InitNWIO():NativeWindowInitOptions
	{
		var nwio:NativeWindowInitOptions = new NativeWindowInitOptions();
		nwio.maximizable = false;
		return nwio;
	}
//==================================================================================================
// public
//==================================================================================================
	/**
	 */
	public function close():void
	{
		if ( this.stage != null )
		{
			this.stage.nativeWindow.close();
		}
	}
	/**
	 */
	public function setApp(obA:ChcPlayerApp):void
	{
		p_obApp = obA;
	}
	/**
	 */
}
}